package com.aleaho.cftrack.presenter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.R;
import com.aleaho.cftrack.activity.DepartmentTreeActivity;
import com.aleaho.cftrack.activity.tree.HeaderViewHolder;
import com.aleaho.cftrack.activity.tree.IconTreeItemHolder;
import com.aleaho.cftrack.entity.DepartmentAndUser;
import com.aleaho.cftrack.http.ApiResponseWraper;
import com.aleaho.cftrack.http.ApiResponseWraperNoList;
import com.aleaho.cftrack.http.NetWorkUtil;
import com.aleaho.cftrack.http.RequestParam;
import com.aleaho.cftrack.http.api.DepartmentTreeApi;
import com.aleaho.cftrack.http.entity.DepartmentTreeEntity;
import com.aleaho.cftrack.utils.Constants;
import com.aleaho.cftrack.utils.TreeUtil;
import com.aleaho.cftrack.view.DepartmentTreeView;
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Aleaho
 * @version 1.0
 * @date 2017/11/13
 * @desc
 */

public class DepartmentTreePresenter {

    private DepartmentTreeView view = null;
    private Context context;
    private AndroidTreeView treeView = null;


    public DepartmentTreePresenter(DepartmentTreeView view, Context context) {
        this.view = view;
        this.context = context;
    }


    /**
     * 获得整个单位的机构人员数据
     */
    public void getAllTreeNodeData() {

        NetWorkUtil.getDepartmentTreeApi().getAllDepartmentTree()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ApiResponseWraper<DepartmentTreeEntity>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(Constants.TAG, e.toString());
                        view.showErrorMessage(e.toString());
                    }

                    @Override
                    public void onNext(ApiResponseWraper<DepartmentTreeEntity> dTreeNodes) {

                        if (Constants.SERVER_ERROR_CODE == dTreeNodes.getCode()) {
                            Log.e(Constants.TAG, dTreeNodes.getMsg());
                            view.showErrorMessage(dTreeNodes.getMsg());
                            return;
                        }

                        List<DepartmentTreeEntity> departmentTreeEntities = dTreeNodes.getData();

                        if (departmentTreeEntities == null) {
                            view.showErrorMessage(context.getResources().getString(R.string.none_department_tree_node_data));
                            return;
                        }
                        converterToTreeNode(departmentTreeEntities);
                    }
                });

    }


    /**
     * 获得单个机构的组织人员结构数据
     *
     * @param id 系统中机构ID编号
     */
    public void getSingleTreeNodeData(int id) {
        NetWorkUtil.getDepartmentTreeApi().getSingleDepartmentTree(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ApiResponseWraperNoList<DepartmentTreeEntity>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(Constants.TAG, e.toString());
                        view.showErrorMessage(e.toString());
                    }

                    @Override
                    public void onNext(ApiResponseWraperNoList<DepartmentTreeEntity> dTreeNodes) {
                        if (Constants.SERVER_ERROR_CODE == dTreeNodes.getCode()) {
                            Log.e(Constants.TAG, dTreeNodes.getMsg());
                            view.showErrorMessage(dTreeNodes.getMsg());
                            return;
                        }

                        DepartmentTreeEntity departmentTreeEntity = dTreeNodes.getData();

                        if (departmentTreeEntity == null) {
                            view.showErrorMessage(context.getResources().getString(R.string.none_department_tree_node_data));
                            return;
                        }
                        List<DepartmentTreeEntity> departmentTreeEntities = new ArrayList<>();
                        departmentTreeEntities.add(departmentTreeEntity);
                        converterToTreeNode(departmentTreeEntities);
                    }
                });
    }


    private void converterToTreeNode(List<DepartmentTreeEntity> entities) {

        //转换为数据

        TreeNode root = TreeNode.root();

        DepartmentTreeEntity rootEntity = TreeUtil.getTheRootDepartment(entities);
        TreeNode rootNode = null;

        if (null == rootEntity) {
            DepartmentAndUser du = new DepartmentAndUser(0, "340104038", "长丰农商银行", "总行", "");
            rootNode = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.icon_yinhang, du.mName, du))
                    .setViewHolder(new HeaderViewHolder(context));

            TreeUtil.entitiesToTreeNode(entities, rootNode, context);
            root.addChild(rootNode);
        } else {
            rootNode = TreeUtil.entityToTreeNode(rootEntity, context);
            TreeUtil.entitiesToTreeNode(entities, rootNode, context);
            root.addChild(rootNode);
        }

        treeView = new AndroidTreeView(context, root);
        treeView.setDefaultAnimation(true);
        treeView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom);
        treeView.setDefaultViewHolder(IconTreeItemHolder.class);
        treeView.setDefaultContainerStyle(R.style.TreeNodeStyleDivided, true);
        view.displayDepartmentTree(treeView);
    }

}
